package com.turtle.security.organizates.controller;

import com.turtle.security.common.annotation.OperateLog;
import com.turtle.security.common.utils.ApiResult;
import com.turtle.security.common.utils.CustomerStringUtils;
import com.turtle.security.organizates.dto.SysMenuDTO;
import com.turtle.security.organizates.entity.SysMenu;
import com.turtle.security.organizates.service.impl.SysMenuServiceImpl;
import com.turtle.security.system.enums.OperateClient;
import com.turtle.security.system.enums.OperateType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统菜单表
 *
 * @author turtle
 * date： 2020-05-29
 */
@RestController
@Api(value = "系统菜单模块", tags = "系统菜单模块")
@RequestMapping("/organizates/menu")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))

public class SysMenuController {
    private final SysMenuServiceImpl sysMenuService;

    /**
     * 添加系统菜单
     *
     * @param sysMenu 系统菜单信息
     */
    @PostMapping("add")
    @ApiOperation(value = "添加系统菜单")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "菜单模块", operateType = OperateType.INSERT, description = "新增菜单")
    public ApiResult menuAdd(@Validated @RequestBody SysMenuDTO sysMenu) {

        sysMenuService.menuAdd(sysMenu);
        return ApiResult.success("添加系统菜单成功");

    }

    /**
     * 更新系统菜单
     *
     * @param sysMenu 系统菜单信息
     * @return
     */
    @PutMapping("update")
    @ApiOperation(value = "更新系统菜单")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "菜单模块", operateType = OperateType.UPDATE, description = "更新菜单")
    public ApiResult menuUpdate(@Validated @RequestBody SysMenuDTO sysMenu) {

        SysMenu menu = sysMenuService.getById(sysMenu.getMenuId());
        if (CustomerStringUtils.isNull(menu)) {
            return ApiResult.error("该菜单不存在");
        }
        sysMenuService.menuUpdate(sysMenu);

        return ApiResult.success("更新系统菜单成功");
    }

}